//
//  CDtopView.m
//  CuriosityDaily3
//
//  Created by 安林 on 16/9/23.
//  Copyright © 2016年 theBestTeam. All rights reserved.
//

#import "CDtopView.h"
#import "Masonry.h"

@implementation CDtopView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setupUI];
    }
    return self;
}

- (void)awakeFromNib
{
    [self setupUI];
}

-(void)setupUI{
    //设置searchbar
    UIButton *searchBar = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, self.bounds.size.width, 64)];
    searchBar.backgroundColor = [UIColor clearColor];

    [self addSubview:searchBar];
    self.searchBar = searchBar;
    [self setFourBtn];
    
   }
-(void)setFourBtn{
    // 创建四个button
 
    
    UIButton *btn1 = [self buttonWithImageName:@"sidebar_setting@2x.png" andTitle:@"设置" andHomeTopViewButtonType:ZFBHomeTopViewButtonTypeScan];
    UIButton *btn2 = [self buttonWithImageName:@"sidebar_switchNight@2x.png" andTitle:@"夜间" andHomeTopViewButtonType:ZFBHomeTopViewButtonTypePay];
    
    UIButton *btn3 = [self buttonWithImageName:@"sidebar_offline@2x.png" andTitle:@"离线" andHomeTopViewButtonType:ZFBHomeTopViewButtonTypeCard];
    
    UIButton *btn4 = [self buttonWithImageName:@"sidebar_share@2x.png" andTitle:@"推荐" andHomeTopViewButtonType:ZFBHomeTopViewButtonTypeXiu];
    
    
    // 添加四个button
    [self addSubview:btn1];
    [self addSubview:btn2];
    [self addSubview:btn3];
    [self addSubview:btn4];
//   //按钮的宽高
//    CGFloat btnW = self.bounds.size.width / 4;
//    btn1.frame = CGRectMake(0, 150, btnW, btnW);
        [btn1 mas_makeConstraints:^(MASConstraintMaker* make) {
            make.top.offset(64);
            make.left.offset(0);
         
        }];
    
        [btn2 mas_makeConstraints:^(MASConstraintMaker* make) {
            make.top.offset(64);
            make.left.equalTo(btn1.mas_right).offset(0);
            make.width.equalTo(btn1);
        }];
    
        [btn3 mas_makeConstraints:^(MASConstraintMaker* make) {
            make.top.offset(64);
            make.left.equalTo(btn2.mas_right).offset(0);
            make.width.equalTo(btn2);
        }];
    
        [btn4 mas_makeConstraints:^(MASConstraintMaker* make) {
            make.top.offset(64);
            make.left.equalTo(btn3.mas_right).offset(0);
            make.right.offset(0);
            make.width.equalTo(btn3);
        }];

    // 监听按钮
    for (int i = 0; i < self.subviews.count; i++) {
        UIButton* btn = self.subviews[i];
        [btn addTarget:self action:@selector(topViewButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    }

}

// 按钮的点击事件
- (void)topViewButtonClick:(UIButton*)sender
{
    
    if ([self.delegate respondsToSelector:@selector(CDtopView:didClickHomeTopViewButtonWithType:andImageName:andLabel:)]) {
        [self.delegate CDtopView:self didClickHomeTopViewButtonWithType:sender.tag andImageName:self.imageView andLabel:self.label];
    }
}
-(UIButton *)buttonWithImageName:(NSString *)imageName andTitle:(NSString *)titleName andHomeTopViewButtonType:(ZFBHomeTopViewButtonType)type
{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    UIImageView *imageView = [[UIImageView alloc]init];
    UILabel *label = [[UILabel alloc]init];
    imageView.frame = CGRectMake(30, 5, 30, 30);
    label.frame = CGRectMake(30, 40, 30, 20);
    imageView.image = [UIImage imageNamed:imageName];
    label.text = titleName;
    label.textColor = [UIColor whiteColor];
    label.font = [UIFont systemFontOfSize:13];
    button.tag = type;
    [button addSubview:imageView];
    [button addSubview:label];
    if (button.tag == 1) {
        self.imageView = imageView;
        self.label = label;
    }
    
    return  button;
}
@end
